package com.ruoyi.oa.controller;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.oa.task.MyTask;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.EmployeeInfo;
import com.ruoyi.oa.domain.vo.EmployeeInfoVo;
import com.ruoyi.oa.domain.bo.EmployeeInfoBo;
import com.ruoyi.oa.service.IEmployeeInfoService;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 员工信息
 *
 * @author ruoyi
 * @date 2023-06-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/info")
public class EmployeeInfoController extends BaseController {

    private final IEmployeeInfoService iEmployeeInfoService;

    /**
     * 查询员工信息列表
     */
    @SaCheckPermission("oa:employeeInfo:list")
    @GetMapping("/list")
    @SaIgnore
    public TableDataInfo<EmployeeInfoVo> list(EmployeeInfoBo bo, PageQuery pageQuery) {
        return iEmployeeInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出员工信息列表
     */
    @SaCheckPermission("oa:employeeInfo:export")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmployeeInfoBo bo, HttpServletResponse response) {
        List<EmployeeInfoVo> list = iEmployeeInfoService.queryList(bo);
        list.forEach(d->{
            SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
            if (ObjectUtil.isNotEmpty(d.getBirthDate())){
                d.setBirthDateForExcel(format.format(d.getBirthDate()));
            }
        });
        ExcelUtil.exportExcel(list, "员工信息", EmployeeInfoVo.class, response);
    }

    /**
     * 获取员工信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("oa:employeeInfo:query")
    @GetMapping("/{id}")
    public R<EmployeeInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iEmployeeInfoService.queryById(id));
    }

    /**
     * 新增员工信息
     */
//    @SaCheckPermission("oa:employeeInfo:add")
    @Log(title = "员工信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @SaIgnore
    @PostMapping()
    public R<EmployeeInfoBo> add(@Validated(AddGroup.class) @RequestBody EmployeeInfoBo bo) {
        return R.ok(iEmployeeInfoService.insertByBo(bo));
    }

    /**
     * 修改员工信息
     */
    @SaCheckPermission("oa:employeeInfo:edit")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @SaIgnore
    @PutMapping()
    public R<EmployeeInfo> edit(@Validated(EditGroup.class) @RequestBody EmployeeInfoBo bo) {
        return R.ok(iEmployeeInfoService.updateByBo(bo));
    }

    /**
     * 删除员工信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("oa:employeeInfo:remove")
    @Log(title = "员工信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iEmployeeInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
